package com.example.dllo.my36kr.ui.adapter;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;
import com.example.dllo.my36kr.R;
import com.example.dllo.my36kr.model.InvestListBean;
import com.squareup.picasso.Picasso;
/**
 * 股权投资的listview 的adapter
 * 我这里4个fragment用着一个adapter
 * Created by CPC_272 on 16/8/16.
 */
public class InvestListAdapter extends BaseAdapter {
    private Context context;
    private InvestListBean data;

    public InvestListAdapter(Context context) {
        this.context = context;
    }

    public void setData(InvestListBean data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data!=null?data.getData().getData().size():0;
    }

    @Override
    public Object getItem(int position) {
        return data!=null ? data.getData().getData().get(position):0;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if (convertView == null) {
            convertView= LayoutInflater.from(context).inflate(R.layout.fragment_chlid_invest,parent,false);
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }
        //取得文字
        holder.tv_title.setText(data.getData().getData().get(position).getCompany_name());
        holder.tv_investcompany.setText(data.getData().getData().get(position).getLead_name());
        holder.tv_present.setText(data.getData().getData().get(position).getCompany_brief());
        //解析图片
        Picasso.with(context).load(data.getData().getData().get(position).getCompany_logo()).into(holder.imag_title);
        Picasso.with(context).load(data.getData().getData().get(position).getFile_list_img()).into(holder.image_big);
        holder.tv_fininsh.setText(data.getData().getData().get(position).getFundStatus().getDesc());
        holder.tv_person.setText(data.getData().getData().get(position).getCf_advantage().get(0).getAdcontent());
        //get(1)是因为这里面有好几条数据 1是json里面的是第二条
        holder.tv_team.setText(data.getData().getData().get(position).getCf_advantage().get(1).getAdcontent());
        holder.tv_adName.setText(data.getData().getData().get(position).getCf_advantage().get(0).getAdname());
        holder.tv_adNametoo.setText(data.getData().getData().get(position).getCf_advantage().get(1).getAdname());
        //取得seekbar的进度数字  不这样写 小数点后面有点多
         int houMuch= (int) (data.getData().getData().get(position).getRate()*100);
//         holder.tv_howMuch.setText((data.getData().getData().get(position).getRate()*100)+"%");
          holder.tv_howMuch.setText(houMuch+"%");
          holder. mSeekBar.setMax(100);

        //进度条的进度
//        if (houMuch <100) {
        Log.d("InvestListAdapter", "holder.mSeekBar.getMax():" + holder.mSeekBar.getMax());
            holder.mSeekBar.setProgress(houMuch);
        Log.d("InvestListAdapter", "holder.mSeekBar.getProgress():" + holder.mSeekBar.getProgress());
//        }

//        holder.mSeekBar.setProgress(100);
          //holder.mSeekBar.setProgress(88);
        //当数小于80的时候给设置为 "认购"
        //大于80的时候的 给 设置为"去看看";
        if (houMuch>=80){
            holder.tv_see.setText("去看看");
             holder.tv_see.setBackgroundColor(Color.WHITE);
        } else if(houMuch<80) {
            holder.tv_see.setText("认购");
            holder.tv_see.setBackgroundColor(Color.YELLOW);
        }
        return convertView;
    }
    class ViewHolder{
        private TextView tv_title, tv_present, tv_howMuch, tv_investcompany, tv_person, tv_team, tv_adName, tv_adNametoo,tv_fininsh;
        private ImageView image_big,imag_title;
        private ProgressBar mSeekBar;
        private Button tv_see;

        public ViewHolder(View view) {
            tv_title = (TextView) view.findViewById(R.id.tv_title_invest);//title
            tv_present = (TextView) view.findViewById(R.id.tv_present_invest);//下面小字
            tv_howMuch = (TextView) view.findViewById(R.id.howMuch_invest);//%这个符号里的数字
            tv_investcompany = (TextView) view.findViewById(R.id.lingTou_invest);//公司名
            tv_person = (TextView) view.findViewById(R.id.person_invest);//创始人后面的字
            tv_team = (TextView) view.findViewById(R.id.team_invest);//团队成员后面的字
            tv_adName = (TextView) view.findViewById(R.id.startPerson);//创始人
            tv_adNametoo = (TextView) view.findViewById(R.id.team);//团队成员
            image_big = (ImageView) view.findViewById(R.id.img_big_invest);//中间大图片
            imag_title = (ImageView) view.findViewById(R.id.img_listtle_invest);//头像
            mSeekBar = (ProgressBar) view.findViewById(R.id.progressBar_invest);//进度条
            tv_fininsh = (TextView) view.findViewById(R.id.finishInvest);//募资 完成 成功 中
            tv_see = (Button) view.findViewById(R.id.toSee_invest);
        }
    }
}
